import axios from 'axios';
import base from './config';
// const baseURL = 'http://localhost:8080/api';
const baseURL= `${base}/api`

export const fetchNotificationsNumService = async () => {
    try {
        const response = await axios.get(`${baseURL}/notifications/unreadCount`);
        return response.data; // 返回通知数量
    } catch (error) {
        console.error('获取通知数量失败:', error);
        throw error;
    }
}

export const listNotificationsService = async (userId, page, pageSize) => {
    try {
        const response = await axios.get(`${baseURL}/notifications`, {
            params: {
              userId: userId,  
              page: page || 1,
              pageSize: pageSize || 10
            }
        });
        return response.data; // 返回通知列表
    } catch (error) {
        console.error('获取通知列表失败:', error);
        throw error;
    }
}

export const markNotificationService = async (notificationId) => {
    try {
        const response = await axios.post(`${baseURL}/notifications/read/${notificationId}`);
        return response.data; // 返回操作结果
    } catch (error) {
        console.error('标记通知为已读失败:', error);
        throw error;
    }
}

export const markAllNotificationsService = async () => {
    try {
        const response = await axios.post(`${baseURL}/notifications/readAll`);
        return response.data; // 返回操作结果
    } catch (error) {
        console.error('标记所有通知为已读失败:', error);
        throw error;
    }
}